<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FmwWxsphController extends Controller
{
    protected $baseUrl = '/api/wxsph/';

    public function index(Request $request)
    {
        $method = str_replace($this->baseUrl, '', parse_url($request->getRequestUri())['path']);
        if (empty($method)) {
            return [];
        }

        $method = str_replace('/', '_', $method);
        if (!method_exists($this, $method)) {
            echo sprintf("[%s] mock function not exists", $method);
            exit;
        }

        return $this->$method($request);
    }

    public function product_list_get(Request $request)
    {
        $next_key = $request->get('next_key', '');
        if (!$next_key) {
            return [
                "errcode" => 0,
                "errmsg" => 'ok',
                "product_ids" => ['63068001'],
                "next_key" => "topage2",
                "total_num" => 1
            ];
        } else {
            return [
                "errcode" => 0,
                "errmsg" => 'ok',
                "product_ids" => [],
                "next_key" => "topagenone",
                "total_num" => 1
            ];
        }
    }

    public function product_get(Request $request)
    {
        $product_id = $request->get('product_id', '');
        if ($product_id != '63068001') {
            return [
                "errcode" => 10020065,
                "errmsg" => '商品未上架'
            ];
        }

        $res = "{\"errcode\":0,\"errmsg\":\"ok\",\"product\":{\"product_id\":\"63068001\",\"out_product_id\":\"\",\"title\":\"微信视频号测试商品A\",\"sub_title\":\"测试\",\"head_imgs\":[],\"desc_info\":{\"imgs\":[]},\"cats\":[{\"cat_id\":\"1\"},{\"cat_id\":\"2\"},{\"cat_id\":\"3\"}],\"attrs\":[{\"attr_key\":\"商品产地\",\"attr_value\":\"广东\"}],\"express_info\":{\"template_id\":\"123456\"},\"status\":5,\"edit_status\":4,\"skus\":[{\"sku_id\":\"6306800101\",\"out_sku_id\":\"WXSPH001\",\"thumb_img\":\"\",\"sale_price\":1,\"stock_num\":20,\"sku_code\":\"WX6306800101\",\"sku_attrs\":[{\"attr_key\":\"颜色\",\"attr_value\":\"红色\"}],\"status\":5}],\"min_price\":1,\"spu_code\":\"WX63068001\"}}";
        return json_decode($res, true);
    }

    public function order_list_get(Request $request)
    {
        $next_key = $request->get('next_key', '');
        if (!$next_key) {
            return [
                "errcode" => 0,
                "errmsg" => 'ok',
                "order_id_list" => ['63062022082181101'],
                "next_key" => "topage2",
                "has_more" => false
            ];
        } else {
            return [
                "errcode" => 0,
                "errmsg" => 'ok',
                "order_id_list" => [],
                "next_key" => "topagenone",
                "has_more" => false
            ];
        }
    }

    public function order_get(Request $request)
    {
        $is_present = false;
        $order_id = $request->get('order_id', '');
        $res = [
            "errcode" => 0,
            "errmsg" => "ok",
            "order" => [
                "order_id" => $order_id,
                "status" => 20,
                "create_time" => 1695189600,
                "update_time" => 1695190200,
                "order_detail" => [
                    "product_infos" => [
                        [
                            "product_unique_id" => $order_id . '_001',
                            "product_id" => '63068001',
                            "sku_id" => '6306800101',
                            "out_sku_id" => 'SKUA00001',
                            "sku_cnt" => 1,
                            "on_aftersale_sku_cnt" => 0,
                            "finish_aftersale_sku_cnt" => 0,
                            "title" => "微信视频号测试商品A",
                            "thumb_img" => "",
                            "sale_price" => 15000,
                            "market_price" => 12000,
                            'is_discounted' => true,
                            'is_free_gift' => false,
                            'estimate_price' => 10000,
                            'real_price' => 10000,
                            'change_sku_info' => [
                                'preshipment_change_sku_state' => 4,
                                'old_sku_id' => '6306800101',
                                'new_sku_id' => '6306800222',
                                'ddl_time_stamp' => time()
                            ]
                        ],
                        [
                            "product_unique_id" => $order_id . '_002',
                            "product_id" => '63068001',
                            "sku_id" => '6306800222',
                            "out_sku_id" => 'SKUA00002',
                            "sku_cnt" => 1,
                            "on_aftersale_sku_cnt" => 0,
                            "finish_aftersale_sku_cnt" => 0,
                            "title" => "微信视频号测试商品B",
                            "thumb_img" => "",
                            "sale_price" => 0,
                            "market_price" => 0,
                            'is_discounted' => true,
                            'is_free_gift' => true,
                            'estimate_price' => 0,
                            'real_price' => 0
                        ],
                    ],
                    "pay_info" => [
                        "prepay_id" => (string)rand(10000000000, 99999999999),
                        "transaction_id" => "wx" . $order_id,
                        "prepay_time" => 1695190200,
                        "pay_time" => 1695190200,
                        "payment_method" => 2,
                    ],
                    "price_info" => [
                        "product_price" => 15000,
                        "order_price" => 10500,
                        "freight" => 500,
                        "discounted_price" => 5000,
                        "is_discounted" => true,
                    ],
                    "delivery_info" => [
                        "address_info" => [
                            "user_name" => "张恒",
                            "postal_code" => "102209",
                            "province_name" => "北京",
                            "city_name" => "北京市",
                            "county_name" => "昌平区",
                            "detail_info" => "北七家大厦12层",
                            "national_code" => "1201",
                            "tel_number" => "16601166306",
                        ],
                        "delivery_product_info" => [

                        ],
                        "ship_done_time" => 0,
                        "deliver_method" => 0,
                        "address_under_review" => [
                            "user_name" => "张恒",
                            "postal_code" => "102209",
                            "province_name" => "北京",
                            "city_name" => "北京市",
                            "county_name" => "昌平区",
                            "detail_info" => "北七家中学",
                            "national_code" => "",
                            "tel_number" => "13693170400",
                        ],
                        "address_apply_time" => 1695191400,
                    ],
                    "coupon_info" => ["user_coupon_id" => "63068901"],
                    "ext_info" => [
                        "customer_notes" => "发中通",
                        "merchant_notes" => "测试订单",
                    ],
                    "sharer_info" => [],
                    "settle_info" => [],
                    "sku_sharer_infos" => [],
                ],
                "aftersale_detail" => [],
                "openid" => "zh0815",
            ],
        ];

        if ($is_present) {
            $res['order']['is_present'] = true;
            $res['order']['present_order_id'] = 'gift' . $order_id;
            $res['order']['present_note'] = '礼物留言';
            $res['order']['status'] = 10;
            //$res['order']['status'] = 12;
            //$res['order']['status'] = 20;
            if (in_array($res['order']['status'], [10, 250])) {
                $res['order']['order_detail']['pay_info']['transaction_id'] = '';
                $res['order']['order_detail']['price_info']['order_price'] = 0;
                unset($res['order']['order_detail']['pay_info']['payment_method']);
            }
            if (in_array($res['order']['status'], [10, 12, 250])) {
                $res['order']['order_detail']['delivery_info'] = [
                    "address_info" => [
                        "user_name" => "",
                        "postal_code" => "",
                        "province_name" => "",
                        "city_name" => "",
                        "county_name" => "",
                        "detail_info" => "***",
                        "national_code" => "",
                        "tel_number" => "",
                    ],
                    "delivery_product_info" => [

                    ],
                    "ship_done_time" => 0,
                    "deliver_method" => 0
                ];
            }
        }

        return $res;
    }

    public function aftersale_getaftersalelist(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok',
            "after_sale_order_id_list" => [],
            "next_key" => "topagenone",
            "has_more" => false
        ];
    }

    public function aftersale_getaftersaleorder(Request $request)
    {
        $after_sale_order_id = $request->get('after_sale_order_id', '');
//        if ($after_sale_order_id != '630622088110101') {
//            return [
//                "errcode" => 10020000,
//                "errmsg" => '售后单号不存在'
//            ];
//        }

        $giftProduct = [
            "product_id" => "63068001",
            "sku_id" => "6306800222",
            "count" => 1,
        ];

        $res = [
            "errcode" => 0,
            "after_sale_order" => [
                "after_sale_order_id" => $after_sale_order_id,
                "status" => "USER_WAIT_RETURN",
                "openid" => "zh0815",
                "order_id" => "20250610003",
                "product_info" => [
                    "product_id" => "63068001",
                    "sku_id" => "6306800101",
                    "count" => 1,
                    "gift_product_list" => [
                        $giftProduct,
                    ],
                ],
                "exchange_product_info" => [
                    "product_id" => "63068001",
                    "old_sku_id" => "6306800101",
                    "new_sku_id" => "6306800222",
                    "product_cnt" => 1,
                ],
                "exchange_delivery_info" => [
                    "waybill_id" => date('YmdHis'),
                    "delivery_id" => '11', // 物流公司id
                    "delivery_name" => '中通快递',
                    "address_info" => [
                        "user_name" => "张恒",
                        "postal_code" => "102209",
                        "province_name" => "北京",
                        "city_name" => "北京市",
                        "county_name" => "昌平区",
                        "detail_info" => "北七家中学",
                        "national_code" => "",
                        "tel_number" => "13693170400",
                        'house_number' => '',
                        'virtual_order_tel_number' => '',
                    ],
                ],
                "details" => [
                    "desc" => "描述",
                    "receive_product" => true,
                    "cancel_time" => 0,
                    "media id list" => [],
                    "tel_number" => "",
                ],
                "refund_info" => ["amount" => 10500, "refund_reason" => 1],
                "return_info" => [
                    "waybill_id" => "",
                    "delivery_id" => "",
                    "delivery_name" => "",
                ],
                "merchant_upload_info" => [
                    "reject_reason" => "",
                    "refund_certificates" => [],
                ],
                "create_time" => 1695193200,
                "update_time" => 1695193200,
                "reason" => "NOT_MATCH_PRODUCT_DESC",
                "refund_resp" => ["code" => "", "ret" => 0, "message" => ""],
                "type" => "EXCHANGE", // REFUND:退款；RETURN:退货退款；EXCHANGE:换货。
            ],
        ];

        return $res;
    }

    public function product_stock_update(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function order_delivery_send(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function order_addressmodify_accept(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function order_addressmodify_reject(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function order_preshipmentchangesku_approve(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function order_preshipmentchangesku_reject(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok'
        ];
    }

    public function logistics_ewaybill_biz_template_get(Request $request)
    {
        $delivery_id = $request->get('delivery_id', '');
        if ($delivery_id && $delivery_id != 'YD') {
            return [
                "errcode" => 0,
                "errmsg" => 'ok',
                "total_template" => []
            ];
        }

        return [
            "errcode" => 0,
            "errmsg" => "ok",
            "total_template" => [
                [
                    "default_template_id" => "66102",
                    "delivery_id" => "YTO",
                    "template_list" => [
                        [
                            "is_default" => false,
                            "template_desc" => "一联单标准模板",
                            "template_id" => "66101",
                            "template_name" => "一联单韵达",
                            "template_type" => "single",
                            "update_time" => "1690426204",
                            "create_time" => "1690426204",
                            "options" => [
                                [
                                    "option_id" => 0,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 1,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 2,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 3,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 4,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 5,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 6,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 7,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                            ],
                        ],
                        [
                            "is_default" => true,
                            "template_desc" => "一联单自定义模板",
                            "template_id" => "66102",
                            "template_name" => "一联单自定义韵达",
                            "template_type" => "xxx",
                            "update_time" => "1690426204",
                            "create_time" => "1690426204",
                            "options" => [
                                [
                                    "option_id" => 0,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 1,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 2,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 3,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => false,
                                ],
                                [
                                    "option_id" => 4,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 5,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 6,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                                [
                                    "option_id" => 7,
                                    "font_size" => 0,
                                    "is_bold" => false,
                                    "is_open" => true,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    public function logistics_ewaybill_biz_order_precreate(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok',
            "ewaybill_order_id" => date('YmdHi')
        ];
    }

    public function logistics_ewaybill_biz_order_create(Request $request)
    {
        $ewaybill_order_id = $request->get('ewaybill_order_id', '');
        if (!$ewaybill_order_id) {
            return [
                "errcode" => 10025017,
                "errmsg" => 'ewaybill_order_id empty'
            ];
        }
        return [
            "errcode" => 0,
            "errmsg" => 'ok',
            "ewaybill_order_id" => $ewaybill_order_id,
            "waybill_id" => sprintf('XCXD_WXSPH__%s', date('YmdHis', time())),
            "delivery_error_msg" => '',
            "print_info" => 'test print info'
        ];
    }

    public function logistics_ewaybill_biz_order_cancel(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok',
            "delivery_error_msg" => ''
        ];
    }

    public function logistics_ewaybill_biz_print_get(Request $request)
    {
        return [
            "errcode" => 0,
            "errmsg" => 'ok',
            "print_info" => 'test print info'
        ];
    }
}
